#!/bin/bash
# power by yiyefangzhou24
# create 2021/10/25
# env centos 7 x64

#shotdown selinux
setenforce 0
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

yum install -y wget gcc gcc-c++ kernel-devel cmake make git

#install redis
cd /opt
wget http://download.redis.io/releases/redis-6.2.6.tar.gz
tar -xzf redis-6.2.6.tar.gz
cd redis-6.2.6
make MALLOC=libc
make PREFIX=/usr/local/redis install
cp redis.conf /usr/local/redis/bin/
sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/bin/redis.conf
cd /etc/systemd/system/
rm -rf redis.service
echo '[Unit]' >> redis.service
echo 'Description=redis-server' >> redis.service
echo 'After=network.target' >>redis.service
echo '[Service]' >> redis.service
echo 'Type=forking' >> redis.service
echo 'ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf' >> redis.service
echo 'PrivateTmp=true' >> redis.service
echo '[Install]' >> redis.service
echo 'WantedBy=multi-user.target' >> redis.service
ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis-cli
ln -s /usr/local/redis/bin/redis-server /usr/bin/redis-server
systemctl daemon-reload

#install hiredis
cd /opt
wget https://github.com/redis/hiredis/archive/v0.14.1.tar.gz
tar -xzf v0.14.1.tar.gz
cd hiredis-0.14.1
make && make install PREFIX=/usr/local

#install libevent
cd /opt
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure -prefix=/usr/local
make && make install

#install mariadb & mariadb-devel
yum install -y mariadb-server mariadb-client mariadb-devel
cp -r /usr/include/mysql/ /usr/local/include/
platform=`uname -m`
if [ $platform='x86_64' ]
then
    cp -r /usr/lib64/mysql/* /usr/local/lib
else
    cp -r /usr/lib/mysql/* /usr/local/lib
fi

#install apache & php & php_redis
yum -y install httpd php php-mysql php-gd php-xml php-mbstring php-devel
cd /usr/local/src
wget http://pecl.php.net/get/redis-2.2.8.tgz
tar -zxvf redis-2.2.8.tgz
cd redis-2.2.8
phpize=`/usr/bin/phpize`    #可使用whereis phpize命令查看路径
echo $phpize | grep 'Configuring for'
if [ $? -ne 0 ];then
    echo '/usr/bin/phpize missing , please use `whereis phpize` to get phpize path'
else
    ./configure --with-php-config=/usr/bin/php-config
    make && make install
    echo 'extension=redis.so' >> /etc/php.ini

    #start mariadb & redis
    systemctl start mariadb.service
    systemctl start redis.service
    systemctl start httpd.service
    systemctl enable httpd.service
    systemctl enable redis.service
    systemctl enable mariadb.service
    systemctl stop firewalld.service
    systemctl disable firewalld.service
fi